package CAIXA_ELETRONICO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



public class ExtratoDao {
	private Connection connection;
	
	public ExtratoDao() {
		    this.connection = new ConnectionFactory().getConnection();
		  }
	public void insert(int id){
		 String sql="insert into tb_extrato"+
	             "(ID,N_EMPRESTIMOS,N_EXTRATOS,N_SAQUES)"+
	             "values(?,?,?,?)";
		 try {
	         
	         
	         PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);
			// seta os valores
	         stmt.setInt(1,id);
	         stmt.setInt(2,0);
	         stmt.setInt(3,0);
	         stmt.setInt(4,0);
	         
	
	         // executa
	         stmt.execute();
	         stmt.close();
	     } catch (SQLException e) {
	         throw new RuntimeException(e);
	     }
	}
	
	public void AtualizarN_emprestimos(int id)throws SQLException{
		PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("UPDATE tb_extrato SET N_EMPRESTIMOS=N_EMPRESTIMOS+1 WHERE ID = "+id);
		  
		int rs = stmt.executeUpdate();
		    
		  stmt.close();
	}
	public void AtualizarN_extratos(int id)throws SQLException{
		PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("UPDATE tb_extrato SET N_EXTRATOS=N_EXTRATOS+1 WHERE ID = "+id);
		  
		int rs = stmt.executeUpdate();
	  
		  stmt.close();
	}
	public void AtualizarN_saques(int id) throws SQLException{
		PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("UPDATE tb_extrato SET N_SAQUES=N_SAQUES+1 WHERE ID = "+id);
		  
		  int rs = stmt.executeUpdate();
		   
		  stmt.close();
	}
	
	public Extrato getExtrato(int id) throws SQLException{
		PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("SELECT N_EMPRESTIMOS,N_SAQUES,N_EXTRATOS FROM tb_extrato WHERE ID = "+id);
		
		Extrato extrato = new Extrato();
		
		ResultSet rs = stmt.executeQuery();
		if(rs.next()){
			extrato.setN_emprestimos(rs.getInt("N_EMPRESTIMOS"));
			extrato.setN_saques(rs.getInt("N_SAQUES"));
			extrato.setN_extratos(rs.getInt("N_EXTRATOS"));
		}
		
		rs.close();
		stmt.close();
		
		return extrato;
		
	}

	public void disconnect() throws SQLException {
		this.connection.close();
		
	}
	
}
